//
//  PublishViewController.m
//  FengyuanXS
//
//  Created by 王冬 on 2020/8/4.
//  Copyright © 2020 王冬. All rights reserved.
//

#import "PublishViewController.h"
#import "SelectImageView.h"
#import "TypeListViewCell.h"
#import <TZImagePickerController/TZImagePickerController.h>
static const CGFloat kLineSpacing = 1.f;   //列间距 |
static const CGFloat kItemSpacing = 1.f;   //item之间的间距  --
static const NSInteger kRowNumber = 3;     //列数
@interface PublishViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,TZImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
@property (weak, nonatomic) IBOutlet UILabel *codeLabel;
@property (weak, nonatomic) IBOutlet UILabel *companyLabel;
@property (weak, nonatomic) IBOutlet UILabel *sectionLabel;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UIView *backView;
@property (weak, nonatomic) IBOutlet SelectImageView *selectImageView;
@property (nonatomic, strong)YMTextView *textView;
@property (nonatomic, strong) NSArray *typeArray;
@property (nonatomic, strong) NSArray *photos;
@property (nonatomic, strong) NSMutableArray *imageArray;
@property (nonatomic, strong) NSMutableArray *selectImageArray;
@property (nonatomic, strong) NSMutableArray *nameArray;
@property (nonatomic ,copy) NSString *type;
@end

@implementation PublishViewController{
    
    // 单选的时候记录选中的索引
    NSInteger _currentSelectedIndex;
}
-(NSMutableArray *)imageArray{
    if (!_imageArray) {
        _imageArray = [NSMutableArray array];
    }
    return _imageArray;
}

-(NSMutableArray *)selectImageArray{
    if (!_selectImageArray) {
        _selectImageArray = [NSMutableArray array];
    }
    return _selectImageArray;
}

-(NSMutableArray *)nameArray{
    if (!_nameArray) {
        _nameArray = [NSMutableArray array];
    }
    return _nameArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"发布诉求";
    self.textView=[[YMTextView alloc]init];
    [self.backView addSubview:self.textView];
    self.textView.maxNumb = 200;
    self.textView.placeholderColor = [UIColor lightGrayColor];
    self.textView.textFont = [UIFont systemFontOfSize:15];
    self.textView.backgroundColor=[UIColor whiteColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.mas_equalTo(0);
    }];
    self.textView.placeholder = @"诉求描述";
    self.textView.isFinishKeyboard=YES;
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing    = kLineSpacing;
    layout.minimumInteritemSpacing = kItemSpacing;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.collectionViewLayout = layout;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView  registerNib:[UINib nibWithNibName:@"TypeListViewCell" bundle:nil] forCellWithReuseIdentifier:@"TypeListViewCell"];
    
    _currentSelectedIndex = 0;
    
    
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.type = @"1";
    [self getType];
    [self getUserData];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return  self.typeArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    TypeListViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TypeListViewCell" forIndexPath:indexPath];
    
    if (indexPath.row == _currentSelectedIndex) {
        cell.isSelect = YES;
    }else{
        cell.isSelect = NO;
    }
    AppealTypeModel *model = self.typeArray[indexPath.row];
  
    cell.titleLabel.text = model.name;
    
    return  cell;
    
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
     AppealTypeModel *model = self.typeArray[indexPath.row];
    if (indexPath.row !=_currentSelectedIndex) {
        _currentSelectedIndex = indexPath.row;
        self.type = model.id;
        NSLog(@"%@",self.type);
        
        [self.collectionView reloadData];
    }
    
}

//每个单元格的大小size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((collectionView.width-10-kItemSpacing*(kRowNumber-1))/kRowNumber, 50);
}

- (IBAction)addImageBtn:(UIButton *)sender {
     if(self.photos.count >=4){
               UIAlertView *aler  = [[UIAlertView alloc]initWithTitle:@"" message:@"最多可以选择4张哦" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
               [aler show];
           }
           TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:4 delegate:self];
           
           // 你可以通过block或者代理，来得到用户选择的照片.
        WS(weakSelf);
           [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray *photos, NSArray *assets,BOOL isSelectOriginalPhoto) {
               weakSelf.selectImageView.images = photos;
               weakSelf.photos = photos;
               __block NSMutableArray *tempArray =[NSMutableArray arrayWithArray:photos];
               weakSelf.selectImageView.deleteItem = ^(NSInteger index) {
                   [tempArray removeObjectAtIndex:index];
                   weakSelf.selectImageView.images = tempArray;
                   weakSelf.photos = tempArray;
               };
               
               [[ApiManager ShardInstance]upLoadImageimageArray:weakSelf.photos resultBlock:^(NSDictionary *data, NSError *error) {
                            NSLog(@"111");
                   
                   [weakSelf.imageArray addObjectsFromArray:data[@"rs"]];
               }];
           }];
        
         
        
           [self presentViewController:imagePickerVc animated:YES completion:nil];
    
}

- (IBAction)commitBtn:(BoardButton *)sender {
    for (int i =0; i< self.imageArray.count; i++) {
        
        [self.nameArray addObject:[NSString stringWithFormat:@"iosp%d",i]];
        
    }
    
    for (int i =0; i< self.imageArray.count; i++) {
        
        [self.selectImageArray addObject:[NSString stringWithFormat:@"android/%@", self.imageArray[i]]];
        
    }
       WS(weakSelf);
       [[ApiManager ShardInstance]appealPoststaff_id:[TokenManager getUserId] type:self.type content:self.textView.textView.text  photo_name:self.nameArray photo_urls:self.selectImageArray resultBlock:^(NSDictionary *data, NSError *error) {
           
           
           BaseModel *model = [BaseModel mj_objectWithKeyValues:data];
           if (model.code == 1) {
               [[ToastManager shardInstance]showToast:@"提交成功"];
               [weakSelf.navigationController popViewControllerAnimated:YES];
           }
           
       }];
    
    
}
#pragma mark - Table view data source





-(void)getType{
    [[ApiManager ShardInstance]appealTypeResultBlock:^(NSDictionary *data, NSError *error) {
        BaseModel *model = [BaseModel mj_objectWithKeyValues:data];
        
        if (model.code ==1) {
            self.typeArray = [AppealTypeModel mj_objectArrayWithKeyValuesArray:data[@"rs"]];
            [self.collectionView reloadData];
        }
        
        
    }];
    
}


-(void)getUserData{
    WS(weakSelf);
    [[ToastManager shardInstance]showSystemProgress:self.view];
    [[ApiManager ShardInstance]userinfoResultBlock:^(NSDictionary *data, NSError *error) {
        UserModel *model = [UserModel mj_objectWithKeyValues:data[@"rs"]];
        [[ToastManager shardInstance]hideProgress:self.view];
        
        weakSelf.nameLabel.text = model.real_name;
        weakSelf.codeLabel.text = model.idcard;
        weakSelf.phoneLabel.text = [TokenManager getToken];
         weakSelf.companyLabel.text = model.company_name;
         weakSelf.sectionLabel.text = model.section_name;
        
    }];
}
@end
